{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "90cf9dd0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(150, 4)\n",
      ".. _iris_dataset:\n",
      "\n",
      "Iris plants dataset\n",
      "--------------------\n",
      "\n",
      "**Data Set Characteristics:**\n",
      "\n",
      "    :Number of Instances: 150 (50 in each of three classes)\n",
      "    :Number of Attributes: 4 numeric, predictive attributes and the class\n",
      "    :Attribute Information:\n",
      "        - sepal length in cm\n",
      "        - sepal width in cm\n",
      "        - petal length in cm\n",
      "        - petal width in cm\n",
      "        - class:\n",
      "                - Iris-Setosa\n",
      "                - Iris-Versicolour\n",
      "                - Iris-Virginica\n",
      "                \n",
      "    :Summary Statistics:\n",
      "\n",
      "    ============== ==== ==== ======= ===== ====================\n",
      "                    Min  Max   Mean    SD   Class Correlation\n",
      "    ============== ==== ==== ======= ===== ====================\n",
      "    sepal length:   4.3  7.9   5.84   0.83    0.7826\n",
      "    sepal width:    2.0  4.4   3.05   0.43   -0.4194\n",
      "    petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)\n",
      "    petal width:    0.1  2.5   1.20   0.76    0.9565  (high!)\n",
      "    ============== ==== ==== ======= ===== ====================\n",
      "\n",
      "    :Missing Attribute Values: None\n",
      "    :Class Distribution: 33.3% for each of 3 classes.\n",
      "    :Creator: R.A. Fisher\n",
      "    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n",
      "    :Date: July, 1988\n",
      "\n",
      "The famous Iris database, first used by Sir R.A. Fisher. The dataset is taken\n",
      "from Fisher's paper. Note that it's the same as in R, but not as in the UCI\n",
      "Machine Learning Repository, which has two wrong data points.\n",
      "\n",
      "This is perhaps the best known database to be found in the\n",
      "pattern recognition literature.  Fisher's paper is a classic in the field and\n",
      "is referenced frequently to this day.  (See Duda & Hart, for example.)  The\n",
      "data set contains 3 classes of 50 instances each, where each class refers to a\n",
      "type of iris plant.  One class is linearly separable from the other 2; the\n",
      "latter are NOT linearly separable from each other.\n",
      "\n",
      ".. topic:: References\n",
      "\n",
      "   - Fisher, R.A. \"The use of multiple measurements in taxonomic problems\"\n",
      "     Annual Eugenics, 7, Part II, 179-188 (1936); also in \"Contributions to\n",
      "     Mathematical Statistics\" (John Wiley, NY, 1950).\n",
      "   - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.\n",
      "     (Q327.D83) John Wiley & Sons.  ISBN 0-471-22361-1.  See page 218.\n",
      "   - Dasarathy, B.V. (1980) \"Nosing Around the Neighborhood: A New System\n",
      "     Structure and Classification Rule for Recognition in Partially Exposed\n",
      "     Environments\".  IEEE Transactions on Pattern Analysis and Machine\n",
      "     Intelligence, Vol. PAMI-2, No. 1, 67-71.\n",
      "   - Gates, G.W. (1972) \"The Reduced Nearest Neighbor Rule\".  IEEE Transactions\n",
      "     on Information Theory, May 1972, 431-433.\n",
      "   - See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al\"s AUTOCLASS II\n",
      "     conceptual clustering system finds 3 classes in the data.\n",
      "   - Many, many more ...\n"
     ]
    }
   ],
   "source": [
    "# 1、读取iris数据集\n",
    "# 从sklearn.datasets导入iris数据加载器\n",
    "from sklearn.datasets import load_iris\n",
    "\n",
    "# 使用加载器读取数据并且存入变量iris\n",
    "iris = load_iris()\n",
    "\n",
    "# 查看数据规模\n",
    "print(iris.data.shape)\n",
    "\n",
    "# 查看数据说明\n",
    "print(iris.DESCR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6cadfe0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2、分割数据集\n",
    "# 从sklearn.cross_validation导入train_test_split用于数据分割  cross_validatio这个包早就不在使用了，划分到了model_selection这个包中\n",
    "from sklearn.model_selection  import train_test_split\n",
    "\n",
    "# 使用train_test_split分割数据集\n",
    "X_train,X_test,Y_train,Y_test = train_test_split(iris.data, iris.target, test_size=0.25, random_state=33)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7a76e591",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3、使用KNN分类器进行预测\n",
    "# 从sklearn.preprocessing导入数据标准化模块\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "# 从sklearn.neighbors导入KNN分类器\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "# 使用KNN分类器预测测试数据的类别\n",
    "knc = KNeighborsClassifier()\n",
    "knc.fit(X_train, Y_train)\n",
    "Y_predict = knc.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5763aba5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.9473684210526315\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "      setosa       1.00      1.00      1.00         8\n",
      "  versicolor       0.85      1.00      0.92        11\n",
      "   virginica       1.00      0.89      0.94        19\n",
      "\n",
      "    accuracy                           0.95        38\n",
      "   macro avg       0.95      0.96      0.95        38\n",
      "weighted avg       0.96      0.95      0.95        38\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 4、评估预测性能\n",
    "# 使用模型自带的评估函数进行准确性评估\n",
    "print('Accuracy:', knc.score(X_test,Y_test))\n",
    "\n",
    "# 使用分类报告模块查看详细分析\n",
    "from sklearn.metrics import classification_report\n",
    "print(classification_report(Y_test, Y_predict, target_names=iris.target_names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad653280",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
